Skip to content

[TESTING] : Gas benchmarks for deploying diamonds#244

Merged
mudgen merged 4 commits intoPerfect-Abstractions:mainfrom
AnInsaneJimJam:gas-benchmarks
Dec 10, 2025
Merged

[TESTING] : Gas benchmarks for deploying diamonds#244
mudgen merged 4 commits intoPerfect-Abstractions:mainfrom
AnInsaneJimJam:gas-benchmarks

Conversation

@AnInsaneJimJam
Copy link
Contributor

Two Diamond implementations:

  • ConstructorDiamond: Accepts FacetCut[] memory in constructor for single-transaction deployment
  • InitializeDiamond: Uses separate initialize() function with FacetCut[] calldata for two-step deployment

Benchmarks

  • Tests 3 scenarios: 10, 50, and 100 facets (each with 12 function selectors)
  • Measures total gas consumption for both approaches
  • Here are the results:
image
  • Approx 31% More Gas Usage in Init Approach ( Init: 4434331, Constructor: 3373293}
  • Approx 6% More Gas Usage in Init Approach (Init: 17453569, Constructor: 16424695}
  • Approx 3% More Gas Usage in Init Approach (Init: 33762974, Constructor: 32808971}

@netlify
Copy link

netlify bot commented Dec 8, 2025

👷 Deploy request for compose-diamonds pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 3ce9ed8

@AnInsaneJimJam
Copy link
Contributor Author

@mudgen @maxnorm Idk why CI checks are failing but it's compilig locally. Can you review it.

@mudgen
Copy link
Contributor

mudgen commented Dec 9, 2025

@AnInsaneJimJam This is very valuable information, thank you. I am very glad to see these gas reports. I looked over how you implemented these things. Looks good, good job.

You can see why is is failing here: https://github.com/Perfect-Abstractions/Compose/pull/244/checks

Maybe it fails because "LibDiamond.sol" doesn't exist anymore in the repo. You don't have to fix this. I mainly need to see the gas statistics data, which I now see, thank you.

I generally see that the constructor method is more gas efficient, even though using a memory array, because it is only one transaction instead of two.

@AnInsaneJimJam
Copy link
Contributor Author

Hey @mudgen, I am very happy that you liked my work. I was messing with the code. I tried 1000 facets ( My thoughts were since percentages are converging towards 0, so at 1000 facets it should be almost 0). But I got suprising result that at 1000 facets, Init approach saved gas !!!!!!. So by binary search, I found that at around ~350 facets the percentage is 0, then it starts going negative. I am digging deep into how gas is calculated to figure out why is this happening

@mudgen
Copy link
Contributor

mudgen commented Dec 9, 2025

@AnInsaneJimJam Cool! Great to experiment.

I am pretty sure I know what is happening, but it is very good to discover for yourself.

  1. Memory costs gas.
  2. The Init version doesn't use memory, it uses calldata.
  3. After a certain point the gas cost for memory dramatically increases. The more you use the more gas costs per word (32 byte memory slot). The formula is this: total gas cost == 3 * words + (words^2) / 512

I am glad to know that 350 facet number.

@AnInsaneJimJam
Copy link
Contributor Author

AnInsaneJimJam commented Dec 9, 2025

Thankyou @mudgen .Yeah, I also discovered the reason ( exponential increase in gas with memory use).
What to do with this PR?
Should I add those additional facets test( 200, 350 500, 1000)?
Should I modify imorts and push without that libdiamond file (so that PR can be merged).?
Or is the work of PR is done?

I think we should merge this into main ( so that anyone can verify the report) and I can help in creating a gas report for our docs website.

what do you think?

@mudgen
Copy link
Contributor

mudgen commented Dec 10, 2025

Should I add those additional facets test( 200, 350 500, 1000)?

That is up to you.

@AnInsaneJimJam

I don't think we need to anything further with this PR so I can close it. I needed the data and you did a great job and got the data.

However, perhaps you have motivation to have this added to the main branch. Do you want this added to the main branch for some reason? If so is it because you want github to track and show your contribution to this repo? If that is the case then I want you to have that and we can merge in your changes into the main repo. After they are merged in and recorded in the system I'll delete them. Your contribution to the repo will still be recorded and shown by the github system.

@AnInsaneJimJam
Copy link
Contributor Author

@mudgen thank you for understanding. Although that was secondary but still the motivation. It will be great if you do so.

@mudgen
Copy link
Contributor

mudgen commented Dec 10, 2025

@AnInsaneJimJam Thank you for this very valuable work. Merged into main branch.

@mudgen mudgen merged commit ae97d25 into Perfect-Abstractions:main Dec 10, 2025
1 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants